package com.google.jessewilson.codesearch.intellij;

import com.google.jessewilson.codesearch.api.CodeFile;
import com.google.jessewilson.codesearch.api.Line;
import com.google.jessewilson.codesearch.api.QueryRunner;
import com.google.jessewilson.codesearch.api.QueryResponse;
import com.google.jessewilson.codesearch.ui.ResultActionsProvider;
import com.google.inject.Inject;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.project.Project;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * All actions from opening a search result, including downloading the file
 * or opening a local file.
 *
 * @author jessewilson
 */
class IntellijResultActionsProvider implements ResultActionsProvider {

  private final OpenFileInIntellij.Factory openFileInIntellijFactory;
  private final Project project;

  @Inject
  public IntellijResultActionsProvider(
      OpenFileInIntellij.Factory openFileInIntellijFactory,
      Project project) {
    this.openFileInIntellijFactory = openFileInIntellijFactory;
    this.project = project;
  }

  public List<Action> actions(final CodeFile codeFile, final Line line, QueryResponse results) {
    List<Action> result = new ArrayList<Action>();
    final OpenFileInIntellij opener = openFileInIntellijFactory.create(results.getProgress(), codeFile.getSearchProvider(), project);

    // actions that show a matching local file
    ProjectFiles projectFiles = codeFile.getCodeFileExtra(ProjectFiles.class);
    for(final VirtualFile virtualFile : projectFiles.getFiles()) {

      // create a label for the action that's easy to read
      String actionLabel;
      VirtualFile parent = virtualFile.getParent();
      if(parent != null) {
        // TODO(jessewilson): don't use the fully-qualified path, use relateive to IPR home
        actionLabel = "Open locally from " + parent.getPath();
      } else {
        actionLabel = "Open locally";
      }

      // add the action
      result.add(new AbstractAction(actionLabel) {
        public void actionPerformed(ActionEvent e) {
          opener.openLocallyAndShow(virtualFile, codeFile, line);
        }
      });
    }

    // actions to download the remote file
    result.add(new AbstractAction("Download and open") {
      public void actionPerformed(ActionEvent e) {
        opener.downloadAndShow(codeFile, line);
      }
    });

    return result;
  }

  public void doDefaultAction(CodeFile codeFile, Line line, QueryResponse results) {
    // don't do anything if the selected line isn't appropriate
    if(codeFile == null) {
      return;
    }
    OpenFileInIntellij opener = openFileInIntellijFactory.create(results.getProgress(), codeFile.getSearchProvider(), project);

    // try a local file first
    ProjectFiles projectFiles = codeFile.getCodeFileExtra(ProjectFiles.class);
    if (!projectFiles.getFiles().isEmpty()) {
      VirtualFile virtualFile = projectFiles.getFiles().get(0);
      opener.openLocallyAndShow(virtualFile, codeFile, line);
      return;
    }

    // if all else failed, download the remote file
    opener.downloadAndShow(codeFile, line);
  }
}
